import java.util.Comparator;
import java.util.PriorityQueue;

class Solution1 {
    public boolean lemonadeChange(int[] bills) {
        int n = bills.length;
        int[] cnt = new int[2]; //5元的数目和10元的数目
        for(int i =0 ; i<n; i++) {
            if(bills[i] == 5) {
                cnt[0]++;
            }else if(bills[i] == 10) {
                if(cnt[0] > 0) {
                    cnt[0] --;
                    cnt[1] ++;
                }else {
                    return false;
                }
            }else if(bills[i] == 20) {
                if(cnt[1] > 0 && cnt[0] > 0) {
                    cnt[1]--;
                    cnt[0]--;
                }else if(cnt[0] >= 3) {
                    cnt[0] -= 3;
                }else {
                    return false;
                }
            }
        }
        return true;
    }
}

class Solution {
    public int halveArray(int[] nums) {
        PriorityQueue<Double> queue = new PriorityQueue<>(new Comparator<Double>() {
            @Override
            public int compare(Double o1, Double o2) {
                if(o1 == o2) return 0;
                else if(o1 > o2)  return -1;
                else  return 1;
            }
        });
        double sum = 0;
        int count = 0;
        for(int x:nums) {
            sum += x;
            queue.add((double)x);
        }

        double s = 0;
        while(s < sum) {
            double x = queue.poll();
            s += x/2;
            queue.add(x/2);
            count++;
        }

        return count;
    }
}

public class Test {
    public static void main(String[] args) {
        int[] bills = {5,5,10,10,20};
        boolean ret = new Solution1().lemonadeChange(bills);
        System.out.println(ret);
    }
}
